################################################################################ 
#
# Counterspeech encouraging users to adopt the perspective of minority 
# groups reduces hate speech and its amplification on social media
#
# Tab A2 - Engagement 
#
################################################################################ 


rm(list = ls())

# Set to save results
setsave = T

################################################################################ 
#  LIBRARIES
################################################################################ 

library(estimatr)
library(plyr)
library(dplyr)
library(tidyr)
library(readr)
library(ggpubr)
library(xtable)

################################################################################ 
#   DATA AND FOLDER
################################################################################ 

wd = ''
wd_res = paste0(wd, '/results')
wd_data =  paste0(wd, '/data')


data = read.csv(paste0(wd_data, '/dataset_analysis.csv'))
data = data[!(data$user_deleted == "yes"),]

# Create week factors 
data = data %>% mutate(
  datetime = ymd_hms(treated_at),
  day = as.factor(as_date(datetime)),
  week = floor_date(datetime, unit = "week"),   
  week = as.factor(week))

# Put to 0 if deleted
data$n_favorites_to_orig_tweet[data$orig_tweet_deleted ==1] = 0
data$n_retweets_to_orig_tweet[data$orig_tweet_deleted == 1] = 0

################################################################################ 
#   VARIABLES
################################################################################

outvars = c('n_favorites_to_orig_tweet', 'n_retweets_to_orig_tweet', 
            'n_favorites_to_orig_tweet_asinh', 'n_retweets_to_orig_tweet_asinh')
outvars_name = c('No. of likes received\nby xenophobic tweet', 'No. of retweets\nof xenophobic tweet',
                 'No. of likes received\nby xenophobic tweet (ihs)', 'No. of retweets\nof xenophobic tweet (ihs)')
treatments = c('empathy', 'traditional', 'analogical', 'perspective_getting', 'disapproval')
treatments_lab = c( 'Empathy', 'Traditional Perspective Taking',  'Analogical Perspective Taking',
                    'Perspective Getting', 'Disapproval')

# Controls in the PAP
data$account_age_cuts = as.factor(cut(data$account_age, breaks=5))
controls = c("account_age_cuts", 'tox_num_pre', "followers_count",
             "pre_treat_tweet_length", "total_tweets_pre", "friends_count")

# Controls in the PAP + Week fixed effects
controls_maj = c("account_age_cuts", 'tox_num_pre', "followers_count",
                 "pre_treat_tweet_length", "total_tweets_pre", "friends_count", "week")

################################################################################ 
#   Scale variables
################################################################################ 

# EXTREME VALUES OPTION: apply ihs
data$n_favorites_to_orig_tweet_asinh = asinh(data$n_favorites_to_orig_tweet)
data$n_retweets_to_orig_tweet_asinh = asinh(data$n_retweets_to_orig_tweet)

data[c(outvars)] = lapply(data[c(outvars)], scale)


################################################################################ 
#   Run the models with and without controls
################################################################################ 


MODELS = list()
MODELS_controls = list()
MODELS_controls_maj = list()

n = 1

for (j in 1:length(outvars)){
  for (i in 1:length(treatments)){
    
    # drop missings
    droper = union(which(is.na(data[outvars[j]])), which(data[outvars[j]]==Inf))
    
    # keep the right treatment and control group
    keeper = union(which(data[treatments[i]] == 1),  which(data$group_number == 5)) 
    keeper = setdiff(keeper, droper)
    
    # select rows
    y = data[keeper, outvars[j]]
    d = data[,treatments[i]][keeper]
    ctr = data[keeper, controls]
    ctr_maj = data[keeper, controls_maj]
    
    # regression
    MODELS[[n]] = lm_robust(y ~ d, se_type='HC3')
    MODELS_controls[[n]] = lm_robust(y ~ ., se_type='HC3', data = cbind(d, ctr))
    MODELS_controls_maj[[n]] = lm_robust(y ~ ., se_type='HC3', data = cbind(d, ctr_maj))
    
    print('Done with: ')
    print(n)
    
    n = n+1
    
  }}

################################################################################ 
#   Save results in a container
################################################################################ 


make_container = function(list_to_be_transformed, outvars, outvars_name){
  MOD = list_to_be_transformed
  
  coef = sapply(MOD, function(m) round(summary(m)$coefficients[2, 1], 3))
  se = sapply(MOD, function(m) round(summary(m)$coefficients[2, 2], 3))
  pval = sapply(MOD, function(m) round(summary(m)$coefficients[2, 4], 3))
  sample = sapply(MOD, function(m) nobs(m))
  
  container = cbind.data.frame(yvar = rep(outvars, each=5),
                              yvar_name = rep(outvars_name, each=5),
                               treat = rep(treatments, time=length(outvars)),
                               treatments_lab = rep(treatments_lab, time=length(outvars)),
                               coef = coef, se=se, pval=pval, 
                               samplesize = sample
  )
  return(container)
}


outcome = make_container(MODELS, outvars, outvars_name)
container_ctr = make_container(MODELS_controls, outvars, outvars_name)
container_ctr_maj = make_container(MODELS_controls_maj, outvars, outvars_name)

if (setsave){write.csv(outcome, paste0(wd_res, '/tables/engagement_result.csv'), row.names = F)}
if (setsave){write.csv(container_ctr, paste0(wd_res, '/tables/engagement_result_ctr.csv'), row.names = F)}
if (setsave){write.csv(container_ctr_maj, paste0(wd_res, '/tables/engagement_result_ctr_maj.csv'), row.names = F)}
